//
//  HMRightViewController.m
//  仿真开发
//
//  Created by B.HOPE on 2017/3/22.
//  Copyright © 2017年 B.HOPE. All rights reserved.
//

#import "HMRightViewController.h"
#import "UIColor+HMAddition.h"
#import "HMRightNavigationController.h"
#import "NewView.h"
#import "HMDefaultsTool.h"
#import "HMLeftViewController.h"

@interface HMRightViewController ()

@property(nonatomic,assign)NSInteger index ;
@property(nonatomic,strong)UIButton*btn;

@end

@implementation HMRightViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addChildController];
    
    NSString *versionKey = @"CFBundleVersion";
    NSString *footVersionKey = [HMDefaultsTool objetForKey:versionKey];
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[versionKey];
    if (![footVersionKey isEqualToString:currentVersion]) {
        [self makeNew];
        
        [HMDefaultsTool saveObject:currentVersion forKey:versionKey];
    }
    
    footVersionKey = currentVersion;
}

- (void)makeNew
{
    NewView *new = [[NewView alloc] initWithFrame:self.view.bounds];
    new.imageNames = [self loadImage];
    [self.view addSubview:new];
}

- (NSArray *)loadImage
{
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:4];
    
    for (NSInteger i = 0; i < 4; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%zd",i+1];
        [arrM addObject:imageName];
    }
    return arrM.copy;
}

- (void) addChildController {
    
    NSArray *childPramameters = @[
                                  @{@"clsName" : @"HomeStoryboard" , @"title" : @"首页",@"icon" : @"1"},
                                  @{@"clsName" : @"HMFindViewController" , @"title" : @"发现",@"icon" : @"2"},
                                  @{@"clsName" : @"HMHealthViewController" , @"title" : @"健康",@"icon" : @"3"},
                                  ];
    
    NSMutableArray *children = [NSMutableArray array];
    for (NSDictionary *dic in childPramameters) {
        // 创建导航自控制器
        UINavigationController *navChild = [self childController:dic];
        [children addObject:navChild];

        
    }
    
    self.viewControllers = children;
}



- (UINavigationController *)childController: (NSDictionary *)dic {
    
    UIViewController *controller;
    
    if ([dic[@"title"] isEqualToString:@"首页"]) {
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
        
            controller  = [sb instantiateViewControllerWithIdentifier:@"sbsb"];
        
        
    } else {
        Class class = NSClassFromString(dic[@"clsName"]);
        controller = [[class alloc]init];
    }
    
//    controller = [[UIViewController alloc]init];
    
    // 设置子控制器标题
    controller.title = dic[@"title"];
    // 设置标签控制器的选中文字为红色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:36/255.0 green:201/255.0 blue:171/255.0 alpha:1]} forState:UIControlStateSelected];
    
    // 设置子控制器图片
    UIImage *image = [[UIImage imageNamed:[NSString stringWithFormat:@"tab_normal_%@",dic[@"icon"]]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    controller.tabBarItem.image = image;
    
    // 设置子控制器选中图片
    UIImage *selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"tab_normal_%@_selected",dic[@"icon"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    controller.tabBarItem.selectedImage=selectedImage;
    
    // 设置子控制添加到导航控制器
    HMRightNavigationController *nav = [[HMRightNavigationController alloc] initWithRootViewController:controller];
    nav.navigationBar.barTintColor = [UIColor colorWithRed:77/255.0 green:194/255.0 blue:156/255.0 alpha:1];
    
    UIBarButtonItem *backIetm = [[UIBarButtonItem alloc] init];
    
    backIetm.title =@"返回";
    controller.navigationItem.backBarButtonItem = backIetm;
    
    // 添加首页左边的按钮
//    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    [leftBtn setImage:[UIImage imageNamed:@"nav_menu_icon" ] forState:UIControlStateNormal];
//    [leftBtn sizeToFit];
//    leftBtn.frame = CGRectMake(0, 0, 30, 30);
    
    [leftBtn addTarget:self action:@selector(childBtnPopController) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *releaseButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    controller.navigationItem.leftBarButtonItem =releaseButtonItem;
    
    
    controller.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    // 返回导航控制器
    // 让导航条不透明
    nav.navigationBar.translucent = NO;
    
  
    

    
    return nav  ;
    
    
}


#pragma mark - 左边按钮的点击事件
- (void) childBtnPopController {
   
    if (_index==1) {
        [_drawer close];
        
        return;
    }
    
    [_drawer open];
    
    
}


-(void)drawerControllerDidOpen:(ICSDrawerController *)drawerController{
    _index++;
}

-(void)drawerControllerDidClose:(ICSDrawerController *)drawerController{
    _index--;
}


//-(void)drawerControllerDidOpen:(ICSDrawerController *)drawerController
//-(void)open




@end
